<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\logic;


use app\common\model\Advertisers;
use app\common\model\BetsLog;
use app\common\model\BonusLog;
use app\common\model\CompanyAccountLog;
use app\common\model\EthAccount;
use app\common\model\GamblerKing;
use app\common\model\GamblerKingAction;
use app\common\model\GamblerKingInvestment;
use app\common\model\GamblerKingInvestmentUser;
use app\common\model\GamblerKingInvestmentUserLog;
use app\common\model\GamesLog;
use app\common\model\InvestmentBonusLog;
use app\common\model\KingAccountLog;
use app\common\model\KingCategory;
use app\common\model\Member;
use app\common\model\ThirdPlanProgramme;
use app\common\model\Transfer;
use app\common\model\UserAccount;
use app\common\model\UserAccountLog;
use app\common\model\UserKingInvestment;
use app\common\model\UserPayType;
use think\Db;

/**
 * 用户接口逻辑
 */
class UserKing extends ApiBase
{


    /**
     * 申请交易员
     */
    public function apply_bet_king($data = [])
    {
        $member = $this->logicApiBase->member;
        if ($member->level_id < 3) {
            return ajaxreturn(1, lang('您会员等级过低，不可申请成为交易员'));
        }
        $validate_result = $this->validateKing->scene('api_add')->check($data);

        if (!$validate_result) {
            return ajaxreturn(1, lang($this->validateKing->getError()));
        }
        $tags_array = explode(',', $data['tags']);
        $cate_ids = KingCategory::where(['id' => ['in', $tags_array]])->group('parent_id')->column('parent_id');
        $data['cates'] = implode(',', $cate_ids);
        $data['cates'] = ',' . $data['cates'] . ',';
        $data['tags'] = ',' . $data['tags'] . ',';
        if (empty($data['id'])) {
            $info = GamblerKing::get(['status' => 1, 'user_id' => $member['id']]);
            if ($info) {
                return ajaxreturn(1, lang('您已提交一次,请勿重复提交'));
            }


        } else {
            $info = GamblerKing::get($data['id']);
            if ($info['state'] == 2) {
                $count = UserKingInvestment::where(['state' => 1, 'king_id' => $info['id']])->count();
                if ($count > 0) {
                    return ajaxreturn(1, lang('您的产品已有人委托不能编辑'), '1');
                }
                GamblerKingInvestmentUser::where(['state' => ['neq', 4], 'king_id' => $info['id']])->delete();
                $data['is_edit'] = 1;
                $data['prev_info'] = json_encode($info);
            }
            $data['is_update'] = 1;
        }
        $data['user_id'] = $member['id'];

        $data['state'] = 1;
        $data['is_investment'] = 0;
        $member->is_king = 0;
        $member->save();
        $reslut = $this->modelGamblerKing->setInfo($data);
        if ($reslut) {
            //发送系统通知
            user_notice($member['id'], '您的交易员（交易员）申请已提交成功，请您耐心等待交易员培训中心的审核结果，请时刻关注您的申请动态信息');
            return ajaxreturn(0, lang('申请成功'));
        } else {
            return ajaxreturn(1, lang('申请失败'));
        }


    }

    /***
     * 获取交易员信息
     */
    public function get_gambler_king_info()
    {
        $member = $this->logicApiBase->member;
        $info = $this->modelGamblerKing->getInfo(['user_id' => $member['id'], 'status' => 1], 'id,name,describe,content,tags,state');
        if ($info) {
            $info['tags_name'] = $info->tags_name;
            $info['investment_ids'] = $this->modelGamblerKingInvestmentUser->where(['user_id' => $member['id'], 'status' => 1, 'state' => 1])->column('investment_id');
            $list = $this->modelGamblerKingInvestment->getList(['status' => 1], '*', 'id desc', false);
            foreach ($list as $k => $v) {
                if (in_array($v['id'], $info['investment_ids'])) {
                    $list[$k]['is_start'] = 1;
                } else {
                    $list[$k]['is_start'] = 0;
                }
                $list[$k]['image'] = $v->image;
                $list[$k]['back_image'] = $v->back_image;

            }
            $info['tags_arr'] = KingCategory::field('id,name')->where(['id' => ['in', explode(',', $info['tags'])]])->select();
            $info['investment_list'] = $list;
        }
        return $info;
    }

    /***
     *交易员产品类型列表
     */
    public function get_gambler_type_list()
    {

        $list = $this->modelGamblerKingInvestment->getList(['status' => 1], '*', 'id desc', false);
        return $list;
    }

    /**
     * 改变产品类型状态
     */

    public function update_king_investment_state($data)
    {
        $member = $this->logicApiBase->member;
        if ($data['state'] != 1 && $data['state'] != 2) {

            return ['status' => 1, 'msg' => lang('参数错误')];
        }
        $info = $this->modelGamblerKingInvestment->getInfo(['id' => $data['id']]);
        if (!$info) {
            return ['status' => 1, 'msg' => lang('参数错误')];
        }
        $king = $this->modelGamblerKing->getInfo(['user_id' => $member['id'], 'state' => 2]);
        if (!$king) {

            return ['status' => 1, 'msg' => lang('您不是交易员')];
        }
        $investment_info = $this->modelGamblerKingInvestmentUser->getInfo(['investment_id' => $data['id'], 'user_id' => $member['id'], 'state' =>1]);

        if ($investment_info) {
            if ($data['state'] == 2) {
                $investment_count = $this->modelUserKingInvestment->where(['user_investment_id' => $investment_info['id'], 'state' => ['in', [0, 5, 6]]])->count();
                if ($investment_count > 0) {
                    return ['status' => 1, 'msg' => lang('有委托不能关闭')];
                }
                $this->modelUserKingInvestment->where(['user_investment_id' => $investment_info['id']])->delete();
                GamblerKingInvestmentUser::where(['id' => $investment_info['id']])->delete();

            }

        } else {
            $round = $this->modelGamblerKingInvestmentUser->where(['investment_id' => $info['id'], 'king_id' => $king['id']])->order('id desc')->value('round') + 0;
            $count=GamblerKingInvestmentUser::where(['investment_id'=>$info['id'],   'king_id'=>$king['id'],'state'=>2])->count();
            $count1=GamblerKingInvestmentUser::where(['investment_id'=>$info['id'],   'king_id'=>$king['id'],'state'=>3])->count();
            $days=$count*($info['cycle']+$info['start_game_time'])+$count1*$info['start_game_time'];
            $minutes=$count*$info['receipt_minute']*60;
            if($days==0){
                $days=$info['cycle'];
            }
            if($minutes==0){
                $minutes=$info['receipt_minute']*60;
            }
            GamblerKingInvestmentUser::create([
                'investment_id' => $info['id'],
                'king_id' => $king['id'],
                'user_id' => $member['id'],
                'state' => 1,
                'round' => $round + 1,
                'price' => $info['price'],
                'max_money' => $info->superior_money,
                'start_time' => strtotime(date('Y-m-d').' '.$info['start_time']),
                'expire_time' => strtotime(date('Y-m-d').' '.$info['start_time'])+$days*86400,
                'start_game_end_int' => strtotime(date('Y-m-d').' '.$info['start_time'])+$days*86400+$minutes,
            ]);
            //推送交易员动态
            GamblerKingAction::create([
                'king_id' => $king['id'],
                'cover_id' => $king->user->cover_id,
                'content' => '我要开始表演拉，大家还没有上车的尽快上车！',
            ]);
        }
        return ['status' => 0, lang('操作成功')];
    }


    /**
     * 改变产品类型状态
     */

    public function update_king_investment_states($data)
    {
        $member = $this->logicApiBase->member;

        $king = $this->modelGamblerKing->getInfo(['user_id' => $member['id'], 'state' => 2]);
        if (!$king) {
            return ['status' => 1, 'msg' => lang('您不是交易员')];
        }


//        if(!$king['is_investment']!=1){
//            return ajaxreturn(1,lang('您已经关闭所有产品类型'));
//        }


        $list = $this->modelGamblerKingInvestment->getList(['status' => 1], '*', 'id desc', false);
        Db::startTrans();
        try {

            foreach ($list as $k => $v) {

                if ($data['state'] == 1) {

                    $investment_info = $this->modelGamblerKingInvestmentUser->getInfo(['investment_id' => $v['id'], 'user_id' => $member['id'], 'state' =>1]);
                    if (!$investment_info) {
                        $round = $this->modelGamblerKingInvestmentUser->where(['investment_id' => $v['id'], 'king_id' => $king])->order('id desc')->value('round') + 0;
                        $count=GamblerKingInvestmentUser::where(['investment_id'=>$v['id'],   'king_id'=>$king,'state'=>2])->count();
                        $count1=GamblerKingInvestmentUser::where(['investment_id'=>$v['id'],   'king_id'=>$king,'state'=>3])->count();
                        $days=$count*($v['cycle']+$v['start_game_time'])+$count1*$v['start_game_time'];
                        $minutes=$count*$v['receipt_minute']*60;
                        if($days==0){
                            $days=$v['cycle'];
                        }
                        if($minutes==0){
                            $minutes=$v['receipt_minute']*60;
                        }
                        GamblerKingInvestmentUser::create([
                            'investment_id' => $v['id'],
                            'king_id' => $king['id'],
                            'user_id' => $member['id'],
                            'state' => 1,
                            'round' => $round + 1,
                            'price' => $v['price'],
                            'max_money' => $v->superior_money,
                            'start_time' => strtotime(date('Y-m-d').' '.$v['start_time']),
                            'expire_time' => strtotime(date('Y-m-d').' '.$v['start_time'])+$days*86400,
                            'start_game_end_int' => strtotime(date('Y-m-d').' '.$v['start_time'])+$days*86400+$minutes,
                        ]);
                    }


                } else {
                    $investment_info = $this->modelGamblerKingInvestmentUser->getInfo(['investment_id' => $v['id'], 'user_id' => $member['id'], 'state' => 1]);
                    if ($investment_info) {
                        $investment_count = $this->modelUserKingInvestment->where(['user_investment_id' => $investment_info['id'], 'state' => ['in', [0, 5, 6]]])->count();
                        if ($investment_count > 0) {
                            Db::rollback();
                            return ['status' => 1, 'msg' => lang('有委托不能关闭')];
                        }
                        GamblerKingInvestmentUser::where(['id' => $investment_info['id']])->delete();

                    }
                }


            }


            if ($data['state'] == 1) {
                //推送交易员动态
                GamblerKingAction::create([
                    'king_id' => $king['id'],
                    'cover_id' => $king->user->cover_id,
                    'content' => '我要开始表演拉，大家还没有上车的尽快上车！',
                ]);
                $king->is_investment = 1;
            } else {
                $king->is_investment = 0;
            }
            $king->save();
            Db::commit();

            return ['status' => 0, lang('操作成功')];
        } catch (\Exception $e) {
            Db::rollback();
//            return ['status'=>1,'msg'=>$e->getMessage()];
            return ['status' => 1, lang('服务器繁忙,请稍后再试')];
        }


    }

    /**
     * 我是交易员
     */
    public function my_king($data)
    {
        $member = $this->logicApiBase->member;
        $where = [];
        if (!empty($data['start_time']) && !empty($data['end_time'])) {

            $where['create_time'] = ['between', [strtotime($data['start_time'] . ' 00:00:00'), strtotime($data['end_time'] . ' 23:59:59')]];
        }
        $info = $this->modelGamblerKing->where(['user_id' => $member['id'], 'status' => 1, 'state' => 2])->find();
        if (!$info) {
            return ajaxreturn(0, lang('您不是交易员'));
        }
        $info['burst_warehouse_count'] = BonusLog::where(['king_id' => $info['id'], 'is_lose_all' => 1])->group('user_investment_id')->count();
        $info['max_burst_warehouse_count'] = get_max_number($info['burst_warehouse_count']);
        $info['burst_warehouse_total'] = BonusLog::where(['king_id' => $info['id'], 'is_lose_all' => 1])->sum('pei_money') + 0;
        $info['max_burst_warehouse_total'] = get_max_number($info['burst_warehouse_total']);
        $info['surplus_total_money'] = $info->get_total_money($where);
        $info['max_surplus_total_money'] = get_max_number($info['surplus_total_money']);
        $info['order_count'] = UserKingInvestment::where(['king_id' => $info['id']])->count();
        $info['max_order_count'] = get_max_number($info['order_count']);
        $info['bonus_count'] = BonusLog::where(['king_id' => $info['id'], 'type' => 2])->count();
        $info['max_bonus_count'] = get_max_number($info['bonus_count']);
        $info['user_bonus_money'] = BonusLog::where(['king_id' => $info['id'], 'type' => 1, 'number' => ['gt', 0]])->sum('number') + 0;
        $info['max_user_bonus_money'] = get_max_number($info['user_bonus_money']);
        $ping_money = GamesLog::where(['king_id' => $info['id'], 'type' => 5, 'state' => 4, 'is_winner' => 3])->count();
        $info['max_ping_count'] = get_max_number($ping_money);
        $info['ping_count'] = $ping_money;
        $info['profit'] = $info->get_profit_bet($where);
        $info['max_profit'] = get_max_number($info['profit']);
        $info['total_deficit'] = $info->get_loss_bet($where);
        $info['max_total_deficit'] = get_max_number($info['total_deficit']);
        $info['total_bonus'] = $info->get_total_bonus($where);
        $info['max_total_bonus'] = get_max_number($info['total_bonus']);
        $list = $this->modelGamblerKingInvestmentUser->alias('a')->field('a.*')->join('__GAMBLER_KING_INVESTMENT__ i', 'a.investment_id = i.id')->where(['a.king_id' => $info['id'], 'a.state' => ['in', [1, 3]], 'a.status' => 1, 'i.status' => 1])->select();   //我的交易员产品
        $info['person_total_money'] = $member->get_total_money($where);
        $info['max_person_total_money'] = get_max_number($info['person_total_money']);
        $info['person_order_count'] = UserKingInvestment::where(['user_id' => $member['id']])->count();
        $info['max_person_order_count'] = get_max_number($info['person_order_count']);
        $info['person_bonus_count'] = BonusLog::where(['user_id' => $member['id'], 'type' => 1])->count();
        $info['max_person_bonus_count'] = get_max_number($info['person_bonus_count']);
        $info['person_burst_warehouse_count'] = BonusLog::where(['user_id' => $member['id'], 'is_lose_all' => 1])->count();
        $info['max_person_burst_warehouse_count'] = get_max_number($info['person_burst_warehouse_count']);
        $info['person_burst_warehouse_total'] = BonusLog::where(['user_id' => $member['id'], 'is_lose_all' => 1])->sum('pei_money') + 0;
        $info['max_person_burst_warehouse_total'] = get_max_number($info['person_burst_warehouse_total']);
        $winn_money = GamesLog::where(['king_id' => $info['id'], 'type' => 5, 'state' => 4, 'is_winner' => 1])->sum('profit') + 0;
        $lose_money = GamesLog::where(['king_id' => $info['id'], 'type' => 5, 'state' => 4, 'is_winner' => 2])->sum('profit') + 0;
        $winn_count = GamesLog::where(['king_id' => $info['id'], 'type' => 5, 'state' => 4, 'is_winner' => 1])->count();
        $lose_count = GamesLog::where(['king_id' => $info['id'], 'type' => 5, 'state' => 4, 'is_winner' => 2])->count();
        $total_money = $winn_money - $lose_money;
        $total_count = $winn_count + $lose_count;
        $info['person_bet_count'] = $total_count;
        $info['person_bet_money'] = $total_money;
        if ($total_money) {
            $info['left_win_percentage'] = format_money($winn_money / $total_money * 100, 2);
            $info['left_lose_percentage'] = format_money(-$lose_money / $total_money * 100, 2);
        } else {
            $info['left_win_percentage'] = 50;
            $info['left_lose_percentage'] = 50;
        }
        if ($total_count) {
            $info['right_win_percentage'] = format_money($winn_count / $total_count * 100, 2);
            $info['right_lose_percentage'] = format_money($lose_count / $total_count * 100, 2);
        } else {
            $info['right_win_percentage'] = 50;
            $info['right_lose_percentage'] = 50;
        }

        foreach ($list as $k => $v) {
            $list[$k]['image'] = $v->investment->image;
            $list[$k]['back_image'] = $v->investment->back_image;
            $list[$k]['pirce'] = $v->investment->price;
            $list[$k]['cycle'] = $v->investment->cycle;
            $list[$k]['superior_money'] = $v->investment->superior_money;
            $list[$k]['user_bonus'] = $v->investment->user_bonus;
            $list[$k]['king_bonus'] = $v->investment->king_bonus;
            $list[$k]['start_expire_time'] = $v->start_time - time();
            $list[$k]['end_expire_time'] = $v->expire_time - time();
            unset($v->investment);
        }
        $info['investment_list'] = $list;
        return $info;


    }


    /**
     * 我是交易员
     */
    public function my_king_workbench($data)
    {
        $member = $this->logicApiBase->member;
        $where = ['status' => 1];
        if (!empty($data['type']) && $data['type'] == 1) {
            $where['state'] = ['in', [2, 3,]];
        } else {
            $where['state'] = 1;
        }

        !empty($data['investment_id']) && $where['investment_id'] = $data['investment_id'];
        $info = $this->modelGamblerKing->field('name,balance,total_money,king_win_account,stop_lose_account,id,credit_score,tags,user_id')->where(['user_id' => $member['id'], 'status' => 1, 'state' => 2])->find();
        if (!$info) {
            return ajaxreturn(0, lang('您不是交易员'));
        }
        $where['king_id'] = $info['id'];
        $info['avtar'] = $member->avtar;
        $info['level_image'] = $member->level->image;
        $info['tags_arr'] = explode(',', $info->tags_name);
        $info['end_entrust_count'] = $info['total_money'];
        $info['max_end_entrust_count'] = get_max_number($info['end_entrust_count']);

        $info['entrust_do_count'] = GamesLog::where(['king_id' => $info['id'], 'type' => 4])->group('user_investment_id')->count();
        $info['max_entrust_do_count'] = get_max_number($info['entrust_do_count']);

        $info['entrust_bonus_number'] = BonusLog::where(['king_id' => $info['id'], 'type' => 1])->sum('number') + 0;
        $info['max_entrust_bonus_number'] = get_max_number($info['entrust_bonus_number']);

        $info['community_number'] = InvestmentBonusLog::where(['king_id' => $info['id']])->sum('area_number') + 0;
        $info['max_community_number'] = get_max_number($info['community_number']);
        $list = $this->modelGamblerKingInvestmentUser->field('investment_id,surplus_money,price,king_win_account,pattern,king_id,stop_win_money_first,stop_win_money,id,start_time,start_game_end_int,expire_time,total_money,round,settlement_time,state')->where($where)->order('state desc,investment_id asc')->select();   //我的交易员产品
        $info['investment_id'] = GamblerKingInvestmentUser::where(['king_id' => $info['id'], 'state' => 3])->value('investment_id') + 0;
        $info['today_bonus_reward'] = InvestmentBonusLog::where(['king_id' => $info['id'], 'bonus' => ['gt', 0], 'create_time' => ['between', [getTodayStart(), getTodayEnd()]]])->sum('bonus') + 0;
        $info['today_bets_count'] = GamesLog::where(['king_id' => $info['id'], 'type' => 4, 'create_time' => ['between', [getTodayStart(), getTodayEnd()]]])->count();
        $info['today_loss_money'] = GamesLog::where(['king_id' => $info['id'], 'type' => 4, 'state' => 4, 'create_time' => ['between', [getTodayStart(), getTodayEnd()]]])->sum('profit') + 0;
        $user_investment_list = InvestmentBonusLog::where(['king_id' => $info['id'], 'bonus' => ['lt', 0], 'create_time' => ['between', [getTodayStart(), getTodayEnd()]]])->select();
        $info['today_loss_count'] = 0;
        foreach ($user_investment_list as $k => $v) {
            $info['today_loss_count'] += $v->userinvestment->order_count;
        }

        foreach ($list as $k => $v) {

            $list[$k]['name'] = $v->investment->data['name'];
            $list[$k]['order_count'] = UserKingInvestment::where(['user_investment_id' => $v['id'], 'state' => ['in', [0, 4, 5, 6, 7]]])->count();
            $list[$k]['pirce'] = $v->investment->price;
            $list[$k]['is_bonus'] = $v->is_bonus;
            if ($v['state'] == 3) {
                $lose_money = GamesLog::where(['user_investment_id' => $v['id'], 'type' => 4, 'state' => 4])->sum('profit') + 0;

                $list[$k]['king_bonus'] = format_money($lose_money / ($v['surplus_money'] + $v['price']) * 100, 2);
                if ($v['pattern'] == 2) {
                    $list[$k]['is_hang_up'] = ThirdPlanProgramme::where(['user_investment_id' => $v['id'], 'state' => 1])->count();
                } else {
                    $list[$k]['is_hang_up'] = 0;
                }
            } else {
                $list[$k]['king_bonus'] = 0;
                $list[$k]['is_hang_up'] = 0;
            }

            $list[$k]['start_expire_time'] = date('Y-m-d H:i:s', $v->start_time);
            $list[$k]['end_expire_time'] = date('Y-m-d H:i:s', $v->expire_time);
            $list[$k]['game_expire_time'] = 0;
            if ($v['state'] == 2) {
                $list[$k]['game_expire_time'] = $v->getData('start_game_end_int') - TIME_NOW;
            } elseif ($v['state'] == 3) {
                $list[$k]['game_expire_time'] = $v->getData('settlement_time') - TIME_NOW;
            } elseif ($v['state'] == 1) {
                $list[$k]['game_expire_time'] = $v->getData('expire_time') - TIME_NOW;
            }

            unset($v->investment);
        }
        $info['investment_list'] = $list;
        return $info;


    }

    /***
     *交易员历史产品
     */
    public function history_king_investment($data)
    {

        $member = $this->logicApiBase->member;
        $where = ['status' => 1];
        if (!empty($data['type'])) {
            switch ($data['type']) {
                case  1:
                    //今日
                    $where['create_time'] = ['between', [getTodayStart(), getTodayEnd()]];

                    break;
                case  2:
                    //昨日
                    $where['create_time'] = ['between', [getTodayStart() - 86400, getTodayEnd() - 86400]];
                    break;
                case  3:
                    //近七天
                    $where['create_time'] = ['between', [getTodayStart() - 86400 * 7, getTodayEnd()]];
                    break;
                case  4:
                    //近30天
                    $where['create_time'] = ['between', [getTodayStart() - 86400 * 30, getTodayEnd()]];


                    break;


            }
        }
        $info = $this->modelGamblerKing->field('name,balance,king_win_account,stop_lose_account,id,credit_score,tags,user_id')->where(['user_id' => $member['id'], 'status' => 1, 'state' => 2])->find();
        if (!$info) {
            return ajaxreturn(0, lang('您不是交易员'));
        }
        $where['king_id'] = $info['id'];
        !empty($data['state']) && $where['state'] = $data['state'];
        !empty($data['investment_id']) && $where['investment_id'] = $data['investment_id'];
        $list = $this->modelGamblerKingInvestmentUser->getList($where, 'investment_id,price,surplus_money,stop_win_money_first,stop_win_money,king_win_account,king_id,id,round,trade_no,create_time,expire_time,settlement_time,total_money,state', 'id desc');   //我的交易员产品
        foreach ($list as $k => $v) {

            $list[$k]['name'] = $v->investment->data['name'];
            $list[$k]['order_count'] = UserKingInvestment::where(['user_investment_id' => $v['id'], 'state' => ['in', [0, 4, 5, 6, 7]]])->count();
            $money = GamesLog::where(['user_investment_id' => $v->id, 'type' => 4])->sum('profit') + 0;
            if ($v['surplus_money'] > 0) {
                $list[$k]['bonus'] = format_money($money / $v['surplus_money'] * 100, 2);
            } else {
                $list[$k]['bonus'] = 0;
            }

            $list[$k]['pirce'] = $v->investment->price;
            $list[$k]['is_bonus'] = $v->is_bonus;
            $list[$k]['start_expire_time'] = date('Y-m-d H:i:s', $v->expire_time);
            $list[$k]['end_expire_time'] = date('Y-m-d H:i:s', $v->settlement_time);
            unset($v->investment);
        }
        return $list;
    }

    /***
     * 历史产品
     */
    public function history_investment()
    {
        $member = $this->logicApiBase->member;
        $info = $this->modelGamblerKing->field('name,balance,king_win_account,stop_lose_account,id,credit_score,tags,user_id')->where(['user_id' => $member['id'], 'status' => 1, 'state' => 2])->find();
        if (!$info) {
            return ajaxreturn(0, lang('您不是交易员'));
        }
        $list = [];
        $investment_list = GamblerKingInvestment::all(['status' => 1]);
        foreach ($investment_list as $k => $v) {
            $arr = [
                'id' => $v['id'],
                'name' => $v['name'],
                'round' => $this->modelGamblerKingInvestmentUser->where(['king_id' => $info['id'], 'investment_id' => $v['id']])->order('id desc')->value('round') + 0,
                'loss_count' => GamesLog::where(['king_id' => $info['id'], 'type' => 4, 'investment_id' => $v['id'], 'is_winner' => 2])->count(),
                'bonus_count' => InvestmentBonusLog::where(['king_id' => $info['id'], 'investment_id' => $v['id'], 'bonus' => ['gt', 0]])->count(),
                'profit' => InvestmentBonusLog::where(['king_id' => $info['id'], 'investment_id' => $v['id'], 'bonus' => ['gt', 0]])->sum('bonus') + 0,
                'entrust_do_count' => GamesLog::where(['king_id' => $info['id'], 'type' => 4, 'investment_id' => $v['id']])->group('user_investment_id')->count(),
                'bets_count' => GamesLog::where(['king_id' => $info['id'], 'type' => 4, 'investment_id' => $v['id']])->count(),
                'lose_total' => -(GamesLog::where(['king_id' => $info['id'], 'type' => 4, 'is_winner' => 2, 'investment_id' => $v['id']])->sum('profit') + 0),
            ];
            $list[] = $arr;
        }
        return $list;
    }


    /**
     * 产品详情
     */


    public function investment_detail($data)
    {
        $info = $this->modelGamblerKingInvestmentUser->getInfo(['id' => $data['id'], 'user_id' => $this->logicApiBase->member['id']]);

        if (!$info) {
            return ajaxreturn(1, lang('参数错误'));
        }
        $arr = [];
        $arr['state'] = $info['state'];
        $arr['id'] = $info['id'];
        $arr['investment_count'] = $info->order_count;

        $arr['profit'] = $info->bonus_money;
        $arr['bonus'] = InvestmentBonusLog::where(['user_investment_id' => $data['id']])->value('user_bonus') + 0;
        $arr['investment_total'] = $info->total_money;
        $arr['investment_days'] = $info->investment->start_game_time;
        $arr['investment_image'] = $info->investment->image;
        $arr['king_bonus'] = $info->investment->king_bonus;
        $arr['user_bonus'] = $info->investment->user_bonus;
        $arr['investment_name'] = $info->investment->data['name'];
        $arr['stop_win_money'] = $info->stop_win_money;
        $arr['stop_loss_rate'] = $info->investment->stop_loss_rate;
        $arr['stop_lose_money'] = $info->surplus_money * $arr['stop_loss_rate'] / 100;
        $arr['winning_rate'] = $info->investment->winning_rate;
        $arr['round'] = $info->round;
        $arr['cycle'] = $info->investment->start_game_time;
        if ($info['state'] == 1) {
            $arr['down_time'] = $info->expire_time - TIME_NOW;
        } else {
            $arr['down_time'] = $info->settlement_time - TIME_NOW;
        }
        $arr['target_profit'] = $info->investment->max_bonus * $info['price'] * ($info->order_count + 1) / 100;
        $arr['king_name'] = $info->king->data['name'];
        $arr['trade_no'] = $info->trade_no;
        $arr['stop_loss_rate'] = $info->investment->stop_loss_rate;
        $arr['average_bonus'] = BonusLog::where(['user_investment_id' => $info['id'], 'type' => 2])->value('number') + 0;
        $arr['total_bonus'] = $arr['average_bonus'] * $info->order_count;

        $king_bonus = InvestmentBonusLog::where(['user_investment_id' => $info['id']])->find();
        $arr['king_bonus_total'] = BonusLog::where(['user_investment_id' => $info['id'], 'type' => 1])->sum('number') + 0;
        $arr['winn_money'] = $arr['king_bonus_total'];
        $arr['money'] = $info['price'];
        $arr['king_area_bonus'] = $king_bonus ? $king_bonus['area_number'] : 0;
        $arr['user_area_bonus'] = $king_bonus ? $king_bonus['user_area_number'] : 0;
        $arr['start_time'] = date('Y-m-d H:i:fs', $info->start_time);
        $arr['end_time'] = date('Y-m-d H:i:s', $info->expire_time);
        $arr['bonus_time'] = date('Y-m-d H:i:s', $info->settlement_time);
        unset($info->investment);
        unset($info->king);
        return $arr;

    }


    /***
     * 交易员余额转入转出
     */

    public function king_balance_transfer($data)
    {

        $uid = $this->logicApiBase->member['id'];
        //        //验证支付密码
        $result = $this->check_password($data['pin_password']);
        switch ($result) {
            case 1:
                return ajaxreturn(1, lang('您的账户操作多次密码验证不正确，如果不是您本人操作，请前往修改密码'));
                break;
            case 2:
                return ajaxreturn(1, lang('您的密码验证错误2次，您的账户为了账户安全，已被冻结，请在30分钟后再次尝试。'));

                break;
            case 3:
                return ajaxreturn(1, lang('您的密码验证错误1次，您还可以尝试1次！'));
                break;


        }

        if (empty($data['number'])) {
            return ajaxreturn(1, lang('请输入数量'));
        }
        if (!is_numeric($data['number'])) {
            return ajaxreturn(1, lang('格式不正确'));
        }
        if ($data['type'] == 1) {
            //转入
            Db::startTrans();
            $account = UserAccount::check(['wallet_id' => 1, 'user_id' => $uid], false);
            $info = $this->modelGamblerKing->where(['user_id' => $uid, 'status' => 1, 'state' => 2])->find();
            $order_count = GamblerKingInvestmentUser::where(['king_id' => $info['id'], 'state' => 3])->count();
            if ($order_count > 0) {
                return ajaxreturn(1, lang('您有一个产品的委托订单在游戏中，不能操作转回，如需要转回，请在游戏结束后操作'));
            }
            if ($data['number'] > $account['balance']) {
                return ajaxreturn(1, lang('总包余额不足'));
            }
            try {
                //扣除用户总包余额
                $poundage_number = 0;
                $number = $data['number'] - $poundage_number;


                $account->balance -= $data['number'];
                $account->save();

                //写入资金流水
                UserAccountLog::create([
                    'user_id' => $uid,
                    'number' => $data['number'],
                    'wallet_id' => $account['wallet_id'],
                    'before_blance' => $account->balance + $data['number'],
                    'after_blance' => $account->balance,
                    'is_add' => 2,
                    'remark' => '总包转入交易员余额',
                    'type' => 16
                ]);

                $info->balance += $number;
                //写入资金流水
                KingAccountLog::create([
                    'user_id' => $uid,
                    'number' => $data['number'],
                    'king_id' => $info['id'],
                    'before_blance' => $info->balance + $data['number'],
                    'after_blance' => $info->balance,
                    'is_add' => 1,
                    'remark' => '总包转入交易员余额',
                    'type' => 2
                ]);


                $info->save();
                Db::commit();
                return ajaxreturn(0, lang('转入成功'));
            } catch (\Exception $e) {

                Db::rollback();

//            return ajaxreturn(0,$e->getMessage());
                return ajaxreturn(1, lang('服务器繁忙,请稍后再试'));
            }


        } else {
            //转出
            Db::startTrans();
            $account = UserAccount::check(['wallet_id' => 1, 'user_id' => $uid], false);
            $info = $this->modelGamblerKing->where(['user_id' => $uid, 'status' => 1, 'state' => 2])->find();
            if ($data['number'] > $info['balance']) {
                return ajaxreturn(1, lang('交易员余额不足'));
            }

            //扣除用户总包余额

            $number = $data['number'];


            $account->balance += $data['number'];
            $account->save();

            //写入资金流水
            UserAccountLog::create([
                'user_id' => $uid,
                'number' => $data['number'],
                'wallet_id' => $account['wallet_id'],
                'before_blance' => $account->balance - $data['number'],
                'after_blance' => $account->balance,
                'is_add' => 1,
                'remark' => '交易员余额转入总包',
                'type' => 16
            ]);
            $info->balance -= $number;

            $info->save();
            //写入资金流水
            KingAccountLog::create([
                'user_id' => $uid,
                'number' => $number,
                'king_id' => $info['id'],
                'before_blance' => $info->balance + $number,
                'after_blance' => $info->balance,
                'is_add' => 2,
                'remark' => '交易员余额转出到总包',
                'type' => 1
            ]);

            Db::commit();
            return ajaxreturn(0, lang('转出成功'));
        }
    }


    /***
     * 交易员手动分红
     */

    public function king_investment_bonus($data)
    {
        try {
            Db::startTrans();
            $info = GamblerKingInvestmentUser::where(['id' => $data['id']])->lock(true)->find();
            $count = BetsLog::where(['user_investment_id' => $data['id']])->count();
            if ($count == 0) {
                return ajaxreturn(1, lang('您还未开始任何记录数据的计划证明您已经执行任务了，必须是有游戏记录后，才能操作分红'));
            }
            //盈利金额
            $profit_money = $info->stop_win_money_first;
            if ($profit_money < $info->price * $info->investment->max_bonus / 100) {
                return ajaxreturn(1, lang('您本次的委托交易未能达到每一位委托会员的5%的收益，您不能提前分红，您还需要未每一位会有赢' . ($info->stop_win_money * $info->investment->max_bonus / 100 - $profit_money) . '元，方可提前分红'));
            } else {
                //下注金额
                $bets = BetsLog::where(['user_investment_id' => $data['id'], 'state' => 1])->count();
                if ($bets > 0) {
                    return ajaxreturn(1, lang('您还有下注没开奖，请开奖后再试'));
                }
            }

            if (!$info) {
                return ajaxreturn(1, lang('参数错误'));
            }
            if (!$info['is_bonus']) {
                return ajaxreturn(1, lang('不符合条件'));
            }
            if ($info['state'] != 3) {
                return ajaxreturn(1, lang('状态错误'));
            }

            $info->get_reward();
            Db::commit();
            return ajaxreturn(0, lang('分红成功'));
        } catch (\Exception $e) {
            Db::rollback();
            return ajaxreturn(0, $e->getMessage());
            return ajaxreturn(1, lang('服务器繁忙,请稍后再试'));
        }
    }


    /**
     * AY智能
     */
    public function auto_bets_start($data){
        $uid = $this->logicApiBase->member['id'];
        $user_investment_id = $data["investment_id"];
        $lottery_id = $data["lottery_id"];
        $play_id = $data["play_id"];
//        $id = $data["id"];
        $user_investment = Db::name("gambler_king_investment_user")->where(["id"=>$user_investment_id])->find();
        Db::startTrans();
        try{
            $user_ids = UserKingInvestment::where(['user_investment_id' => $user_investment_id, 'state' => 6])->column('user_id');
            $data = array(
                "user_investment_id" =>$user_investment_id,
                "lottery_id"=>$lottery_id,
                "king_id"=>$user_investment["king_id"],
                "play_id"=>$play_id,
                "wait_users"=>json_encode($user_ids),
                "status"=>0,
                "create_time"=>time(),
                "update_time"=>time()
            );
            $res = Db::name("auto_bets_process")->insert($data);
            Db::name("gambler_king_investment_user")->where(["id"=>$user_investment_id])->update(["state"=>3]);
            Db::commit();
            return ajaxreturn(0, 'OK',$res);
        }catch(\Exception $e){
            return ajaxreturn(1, $e->getMessage());
        }
    }

    /**结束进程
     * @param $data
     * @return array
     */
    public function auto_bets_end($data){
        $id = $data["id"];
        Db::startTrans();
        try{
            Db::name("auto_bets_process")->where(["id"=>$id])->update(["status"=>1]);
            Db::commit();
            return ajaxreturn(1, 'OK');
        }catch (\Exception $e){
            return ajaxreturn(0, $e->getMessage());
        }
    }

    /**
     *
     */
    public function auto_bets_detail($data){
        $uid = $this->logicApiBase->member['id'];
        $king_id = Db::name("gambler_king")->where(["user_id"=>$uid])->find()["id"];
        $detail = Db::name("auto_bets_process")->where(["king_id"=>$king_id,"status"=>0])->find();
        return ajaxreturn(0, 'OK',$detail);
    }





    /***
     * 交易员开始游戏
     */

    public function king_start_game($data)
    {

        $uid = $this->logicApiBase->member['id'];
        try {
            Db::startTrans();
            $info = GamblerKingInvestmentUser::where(['id' => $data['id']])->lock(true)->find();
            if (!$info) {
                return ajaxreturn(1, lang('参数错误'));
            }
            if ($info['state'] != 2) {
                return ajaxreturn(1, lang('状态错误'));
            }
            if ($data['type'] == 1) {
                $order_count = GamblerKingInvestmentUser::where(['king_id' => $info['king_id'], 'state' => 3])->count();
                if ($order_count > 0) {
                    return ajaxreturn(1, lang('您有一个产品的委托订单在游戏中，不能开始游戏游戏，请结束结束后操作'));
                }
                $info->state = 3;
                $info->settlement_time = TIME_NOW + $info->investment->start_game_time * 86400;
                $account = UserAccount::check(['wallet_id' => 1, 'user_id' => $uid], false);
                $king = $this->modelGamblerKing->where(['user_id' => $uid, 'status' => 1, 'state' => 2])->find();
                $number = $info['king_win_account'] / $info['order_count'];
                if ($number > $account['balance']) {
                    return ajaxreturn(1, lang('总包余额不足,无法接单，还需') . format_money($number - $account['balance']) . lang('元才能接单'));
                }

                //扣除用户总包余额
                $poundage_number = 0;

                $account->balance -= $number;
                $account->save();

                //写入资金流水
                UserAccountLog::create([
                    'user_id' => $uid,
                    'number' => $number,
                    'wallet_id' => $account['wallet_id'],
                    'before_blance' => $account->balance + $number,
                    'after_blance' => $account->balance,
                    'is_add' => 2,
                    'remark' => '交易员接单自动转入转入交易员余额',
                    'type' => 16
                ]);

                $king->king_win_account += $number;
                $king->balance += $number;
                //写入资金流水
                KingAccountLog::create([
                    'user_id' => $uid,
                    'number' => $number,
                    'king_id' => $king['id'],
                    'before_blance' => $king->balance + $number,
                    'after_blance' => $king->balance,
                    'is_add' => 1,
                    'remark' => '交易员接单自动转入转入交易员余额',
                    'type' => 2
                ]);

                $king->stop_lose_account = $info['stop_lose_account'];
                $king->king_win_account = $info['king_win_account'] + $number;
                $king->save();
                UserKingInvestment::where(['user_investment_id' => $data['id'], 'state' => 5])->update(['state' => 6]);
                $king_action = [
                    'king_id' => $info->king_id,
                    'user_investment_id' => $info->id,
                    'cover_id' => $info->user->cover_id,
                    'content' => '我要开始交易啦',
                    'type' => 2
                ];
                $user_ids = UserKingInvestment::where(['user_investment_id' => $data['id'], 'state' => 6])->column('user_id');
                $leader_ids = Member::where(['id' => ['in', $user_ids]])->group('leader_id')->column('leader_id');
                $member = new Member();
                $member->upgrade_level($leader_ids);
                GamblerKingAction::create($king_action);
                $info->save();

                Db::commit();
                return ajaxreturn(0, lang('成功'));
            } else {

                $user_investment = UserKingInvestment::where(['user_investment_id' => $data['id'], 'state' => 5])->select();

                foreach ($user_investment as $ke => $va) {
                    $user_account = UserAccount::where(['user_id' => $va['user_id'], 'wallet_id' => 3])->lock(true)->find();
                    $user_account->balance += $va['money'];
                    $user_account->freeze_balance -= $va['money'];
                    $user_account->save();
                    //写入资金流水
                    $account_log = [
                        'user_id' => $va['user_id'],
                        'number' => $va['money'],
                        'wallet_id' => 3,
                        'is_add' => 1,
                        'type' => 8,
                        'investment_id' => $va['investment_id'],
                        'bonus' => $va->investment->user_bonus,
                        'pei_money' => $va['money'],
                        'before_blance' => $user_account->balance - $va['money'],
                        'after_blance' => $user_account->balance,
                        'remark' => '交易员关闭产品退回本金',
                    ];
                    UserAccountLog::create($account_log);
                    user_notice($va->user_id, '会员您好，你在' . $va->create_time . '时间提交给' . $info->king->data['name'] . '交易员的' . $info->investment->data['name'] . '产品，因交易员的时间因素或者当前的市场行情等因素，无法完成您提交的委托任务，已被交易员手动取消委托任务，委托金额已返回到您的理财包');
                }
                $info->state = 4;
                $info->save();
                $roud=self::where(['investment_id'=>$info->investment_id,   'king_id'=>$info->king_id,])->order('id desc')->value('round');
                //重新创建产品
                $count=GamblerKingInvestmentUser::where(['investment_id'=>$info->investment_id,   'king_id'=>$info->king_id,'state'=>2])->count();
                $count1=GamblerKingInvestmentUser::where(['investment_id'=>$info->investment_id,   'king_id'=>$info->king_id,'state'=>3])->count();
                $days=$count*($info->investment['cycle']+$info->investment['start_game_time'])+$count1*$info->investment['start_game_time'];
                $minutes=$count*$info->investment['receipt_minute']*60;
                if($days==0){
                    $days=$info->investment['cycle'];
                }
                if($minutes==0){
                    $minutes=$info->investment['receipt_minute']*60;
                }
                GamblerKingInvestmentUser::create([
                    'investment_id' => $info->investment_id,
                    'king_id' => $info->king_id,
                    'user_id' => $info->user_id,
                    'state' => 1,
                    'round' => $roud+ 1,
                    'price' => $info->investment->price,
                    'max_money' => $info->investment->superior_money,
                    'start_time' => strtotime($info->investment['start_time']),
                    'expire_time' => strtotime($info->investment['start_time']) + $days * 86400,
                    'start_game_end_int' => strtotime($info->investment['start_time']) +$days * 86400 +$minutes,
                ]);
                GamblerKing::where(['id' => $info['king_id']])->update(['king_win_account' => 0, 'stop_lose_account' => 0]);
                UserKingInvestment::where(['user_investment_id' => $data['id'], 'state' => 5])->update(['state' => 3]);


            }
            Db::commit();
            return ajaxreturn(0, lang('成功'));
        } catch (\Exception $e) {
            Db::rollback();

//            return ajaxreturn(0,$e->getMessage());
            return ajaxreturn(1, lang('服务器繁忙,请稍后再试'));
        }

    }


    /**
     * 赌余额流水
     */
    public function king_account_log($data)
    {
        $member = $this->logicApiBase->member;
        $info = $this->modelGamblerKing->where(['user_id' => $member['id'], 'status' => 1, 'state' => 2])->find();
        if (!$info) {
            return ajaxreturn(0, lang('您不是交易员'));
        }
        $where = ['king_id' => $info['id']];
        switch ($data['type']) {
            case 1:
                //今天

                $where['create_time'] = ['between', [getTodayStart(), getTodayEnd()]];
                break;
            case 2:
                //昨天
                $where['create_time'] = ['between', [getTodayStart() - 86400, getTodayEnd() - 86400]];
                break;
            case 3:
                //  前天
                $where['create_time'] = ['between', [getTodayStart() - 86400 * 2, getTodayEnd() - 86400 * 2]];

                break;
            case 4:
                //近7天
                $where['create_time'] = ['between', [getTodayStart() - 86400 * 7, TIME_NOW]];

                break;
            case 5:

                //近15天
                $where['create_time'] = ['between', [getTodayStart() - 86400 * 15, TIME_NOW]];

                break;
            case 6:
//              //近30天
                $where['create_time'] = ['between', [getTodayStart() - 86400 * 30, TIME_NOW]];
                break;
        }

        if (!empty($data['start_time'])) {
            if (!empty($data['end_time'])) {
                $where['create_time'] = ['between', [strtotime($data['start_time'] . ' 00:00:00'), strtotime($data['end_time'] . ' 23:59:59')]];

            } else {
                $where['create_time'] = ['between', [strtotime($data['start_time'] . ' 00:00:00'), strtotime($data['start_time'] . ' 23:59:59')]];

            }
        }
        !empty($data['state']) && $where['type'] = $data['state'];
        $list = $this->modelKingAccountLog->getList($where, '*', 'id desc');
        return $list;

    }


    /**
     * 交易员可用余额流水
     */
    public function king_balance_log($data)
    {
        $member = $this->logicApiBase->member;
        $info = $this->modelGamblerKing->where(['user_id' => $member['id'], 'status' => 1, 'state' => 2])->find();
        if (!$info) {
            return ajaxreturn(0, lang('您不是交易员'));
        }
        $where = ['king_id' => $info['id']];
//        $where['entrust_count']=['gt',0];
        switch ($data['type']) {
            case 1:
                //今天

                $where['create_time'] = ['between', [getTodayStart(), getTodayEnd()]];
                break;
            case 2:
                //昨天
                $where['create_time'] = ['between', [getTodayStart() - 86400, getTodayEnd() - 86400]];
                break;
            case 3:
                //  前天
                $where['create_time'] = ['between', [getTodayStart() - 86400 * 2, getTodayEnd() - 86400 * 2]];

                break;
            case 4:
                //近7天
                $where['create_time'] = ['between', [getTodayStart() - 86400 * 7, TIME_NOW]];

                break;
            case 5:

                //近15天
                $where['create_time'] = ['between', [getTodayStart() - 86400 * 15, TIME_NOW]];

                break;
            case 6:
//              //近30天
                $where['create_time'] = ['between', [getTodayStart() - 86400 * 30, TIME_NOW]];
                break;
        }

        if (!empty($data['start_time'])) {
            if (!empty($data['end_time'])) {
                $where['create_time'] = ['between', [strtotime($data['start_time'] . ' 00:00:00'), strtotime($data['end_time'] . ' 23:59:59')]];

            } else {
                $where['create_time'] = ['between', [strtotime($data['start_time'] . ' 00:00:00'), strtotime($data['start_time'] . ' 23:59:59')]];

            }
        }
        !empty($data['state']) && $where['type'] = $data['state'];
        $list = $this->modelKingBalanceLog->getList($where, '*', 'id desc');
        return $list;

    }


    /***
     * 个人游戏统计
     */
    public function person_game_statistics($data)
    {
        $member = $this->logicApiBase->member;
        switch ($data['type']) {
            case 1:
                //今天

                $where['create_time'] = ['between', [getTodayStart(), getTodayEnd()]];
                $where1['create_time'] = ['between', [getTodayStart(), getTodayEnd()]];
                break;
            case 2:
                //昨天
                $where['create_time'] = ['between', [getTodayStart() - 86400, getTodayEnd() - 86400]];
                $where1['create_time'] = ['between', [getTodayStart() - 86400, getTodayEnd() - 86400]];
                break;
            case 3:
                //近7天
                $where['create_time'] = ['between', [getTodayStart() - 86400 * 7, TIME_NOW]];
                $where1['create_time'] = ['between', [getTodayStart() - 86400 * 7, TIME_NOW]];

                break;

            case 4:
//              //近30天
                $where['create_time'] = ['between', [getTodayStart() - 86400 * 30, TIME_NOW]];
                $where1['create_time'] = ['between', [getTodayStart() - 86400 * 30, TIME_NOW]];
                break;
        }

        if (!empty($data['start_time'])) {
            if (!empty($data['end_time'])) {
                $where['create_time'] = ['between', [strtotime($data['start_time'] . ' 00:00:00'), strtotime($data['end_time'] . ' 23:59:59')]];
                $where1['create_time'] = ['between', [strtotime($data['start_time'] . ' 00:00:00'), strtotime($data['end_time'] . ' 23:59:59')]];
            } else {
                $where['create_time'] = ['between', [strtotime($data['start_time'] . ' 00:00:00'), strtotime($data['start_time'] . ' 23:59:59')]];
                $where1['create_time'] = ['between', [strtotime($data['start_time'] . ' 00:00:00'), strtotime($data['start_time'] . ' 23:59:59')]];

            }
        }
        $where['user_id'] = $member['id'];
        $where['state'] = ['lt', 4];
        $where1['user_id'] = $member['id'];
        $where1['state'] = ['gt', 1];
        $info['bets_money'] = BetsLog::where($where)->sum('money') + Db::name('investment_bet_log')->where($where1)->sum('number') + 0;
        $info['reward_money'] = BetsLog::where($where)->sum('reward_money') + Db::name('investment_bet_log')->where($where1)->sum('reward_money') + 0;

        $where['state'] = 2;


        $where['user_id'] = $member['id'];
        unset($where['state']);
        $where['type'] = 6;
        $info['return_point'] = GamesLog::where($where)->sum('number') + 0;
        $where['type'] = 4;

        $info['profit'] = GamesLog::where($where)->sum('profit') + Db::name('investment_bet_log')->where($where1)->sum('profit') + 0;

        return $info;
    }


}
